package com.cx.controller;

import com.cx.pojo.GroupSponsor;
import com.cx.service.GroupGoodsService;
import com.cx.service.GroupMemberService;
import com.cx.service.GroupSponsorService;
import com.cx.util.RandomSix;
import com.cx.util.RequestResult;
import com.cx.vo.GroupListVO;
import com.cx.vo.MyGroupInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("groupsponsor")
public class GroupSponsorController {

    @Autowired
    GroupSponsorService groupSponsorService;

    @Autowired
    GroupMemberService groupMemberService;

    @Autowired
    GroupGoodsService groupGoodsService;

    /**
     * 发起团购
     * @param groupSponsor
     * @return
     */
    @RequestMapping("creategroup")
    @ResponseBody
    @Transactional
    public RequestResult createGroup(GroupSponsor groupSponsor){

        Integer count = groupMemberService.countGroupByGoodId(groupSponsor.getUserId(),groupSponsor.getGroupGoodId());
        if (count > 0){
            return new RequestResult(-1,"该商品您有正在进行中的拼团，不可创建拼团",null);
        }

        //团号，随机生成的6位数
        groupSponsor.setGroupNo(RandomSix.randomSix());

        Calendar calendar = Calendar.getInstance();
        //拼团开始时间
        System.out.println("开始时间" + calendar.getTime());
        groupSponsor.setStartTime(calendar.getTime());
        groupSponsor.setJoinNum(1);
        //状态为1，组团中
        groupSponsor.setGroupSponsorStatus(1);
        //结束时间，默认五天后
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) + 5);
        System.out.println("结束时间" + calendar.getTime());
        groupSponsor.setEndTime(calendar.getTime());

        groupSponsor.setCreateTime(new Date());
        groupSponsor.setUpdateTime(new Date());

        int affectedRows = groupSponsorService.insertGroup(groupSponsor);
        if (affectedRows > 0){
            return new RequestResult(1,"创建拼团成功",null);
        } else {
            return new RequestResult(2,"创建拼团失败",null);
        }
    }


    /**
     * 根据商品id查询团购列表
     * @return
     */
    @RequestMapping("selectgroupbygroupgoodid")
    @ResponseBody
    public RequestResult selectGroupByGroupGoodId(Integer groupGoodId){

        //查询之前先更新团购列表状态，
        Integer affectedRows = groupSponsorService.checkAndUpdateStatus();

        List<GroupListVO> groupListVOList = groupSponsorService.selectGroupByGroupGoodId(groupGoodId);
        System.out.println(groupListVOList);
        return new RequestResult(1,"查询成功",groupListVOList);

    }

    @RequestMapping("failandupdatestatus")
    public RequestResult failAndUpdateStatus(Integer groupSponsorId){
        Integer affectedRows = groupSponsorService.failAndUpdateStatus(groupSponsorId);
        if (affectedRows > 0){
            return new RequestResult(1,"状态更新成功",null);
        }
        return new RequestResult(2,"状态更新失败",null);
    }

    @RequestMapping("groupsuccessandupdatestatus")
    @ResponseBody
    public RequestResult updateStatus(Integer groupSponsorId,Integer groupGoodId){
        Integer affectedRows = groupSponsorService.successAndUpdateStatus(groupSponsorId);
        Integer affectedRows2 =groupGoodsService.updateGroupNumber(groupGoodId);
        if (affectedRows > 0 && affectedRows2 > 0){
            return new RequestResult(1,"状态更新成功",null);
        }
        return new RequestResult(2,"状态更新失败",null);
    }

    @RequestMapping("selectmygroupinfo")
    @ResponseBody
    public RequestResult selectMyGroupInfo(Integer userId,Integer status){
        List<MyGroupInfoVO> myGroupInfoVOList = groupSponsorService.selectMyGroupInfo(userId,status);
        return new RequestResult(1,"查询成功",myGroupInfoVOList);
    }

}
